home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / RAVE SDK 1.06 GM for MacOS / Example Projects / GameScene / GSUtilities.c < prev    next >
Encoding:
Text File  |  1996-03-18  |  2.7 KB  |  113 lines  |  [TEXT/ALFA]

  1. // ===========================================================================
  2. //    
  3. //  GSUtilities.c 
  4. //    
  5. //    Copyright (C) 1996 Apple Computer, Inc.  All rights reserved.
  6. //
  7. // ===========================================================================
  8.  
  9.  
  10. // ===========================================================================
  11. //    Includes
  12. // ===========================================================================
  13.  
  14. #include <stdlib.h>
  15.  
  16. #if (kQAPlatform == kQAMacOS)
  17.     #include <Files.h>
  18.     #include <QuickDraw.h>
  19.     #include <Displays.h>
  20.     #include <Processes.h>
  21. #endif
  22.  
  23. #include "GSUtilities.h"
  24.  
  25.  
  26. #if (kQAPlatform == kQAMacOS)
  27.  
  28. // ===========================================================================
  29. //    GSFindDeepestMacDisplay
  30. // ===========================================================================
  31. // Finds the first screen with the largest greatest depth.
  32. //
  33. // RETURNS: handle to the screen device
  34. //
  35.     GDHandle
  36. GSFindDeepestMacDisplay(void)
  37. {
  38.     GDHandle                 colorGDevice = nil;
  39.     GDHandle                 deepestGDevice = nil;
  40.     long                     deviceDepth = 0;
  41.     long                     maxDepth = 0;
  42.  
  43.     for (colorGDevice = DMGetFirstScreenDevice(dmOnlyActiveDisplays); 
  44.             colorGDevice;
  45.             colorGDevice = DMGetNextScreenDevice(colorGDevice, dmOnlyActiveDisplays)) {
  46.         deviceDepth = (**(**colorGDevice).gdPMap).pixelSize;
  47.         
  48.         if (deviceDepth > maxDepth) {
  49.             maxDepth = deviceDepth;
  50.             deepestGDevice = colorGDevice;
  51.         }
  52.     }
  53.  
  54.     return deepestGDevice;
  55. }
  56.  
  57. #endif
  58.  
  59.  
  60. // ===========================================================================
  61. //    GSIsPowerOf2
  62. // ===========================================================================
  63. // Determines whether a number is a power of 2.
  64. //
  65. // inNumber: the number to check
  66. // outPower: if not nil, on exit it points to the base-2 log of inNumber
  67. //
  68. // RETURNS: true if inNumber is a power of 2, false otherwise
  69. //
  70.     Boolean
  71. GSIsPowerOf2(
  72.     unsigned long             inNumber,
  73.     unsigned short*         outPower)
  74. {
  75.     unsigned short             power = 0;
  76.     Boolean                 isPowerOf2;
  77.     
  78.     if (inNumber == 0) {
  79.             // zero's not a power of 2 and the loop below won't end if the number
  80.             // is zero, so bail here
  81.         return false;
  82.     }
  83.     
  84.         // loop until we find a 1 in the lowest bit
  85.     while ((inNumber & 1) == 0) {
  86.         inNumber >>= 1;
  87.         power++;
  88.     };
  89.     
  90.         // get rid of the 1 we just found
  91.     inNumber >>= 1;
  92.     
  93.         // if inNumber is a power of 2, it should now be 0.  if it has any
  94.         // higher bits set, it won't be 0 and it won't be a power of 2
  95.     isPowerOf2 = (inNumber == 0);
  96.     
  97.     if (isPowerOf2 && (outPower != nil)) {
  98.         *outPower = power;
  99.     }
  100.     
  101.     return isPowerOf2;
  102. }
  103.  
  104.  
  105. // ===========================================================================
  106. //    GSRandomFloat
  107. // ===========================================================================
  108.     float 
  109. GSRandomFloat(void)
  110. {
  111.     return (rand() / (float) RAND_MAX);
  112. }
  113.